package my.app.com.baidu.rigel.pss.app1;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import my.java.io.StringFilter;

public class IdeaStringFilter implements StringFilter {
	public static final String IDEA_REGEX = "\\{关键词\\}\\{";
	public static final String IDEA_REPLACEMENT = "{";
	private static final Pattern pattern = Pattern.compile(IdeaStringFilter.IDEA_REGEX);
	private static int size = 4;
	
	private static String INPUT_SPLITER_REGEX = "\\t";
	
	private static String OUTPUT_SPLITER = "\t";
	
	private static String OUTPUT_NULL = "NULL";
	
	private static String OUTPUT_KEYWORD_SPLITER = "##";
	
	private static char IDEA_KEYWORD_START = '{';
	
	private static char IDEA_KEYWORD_END = '}';
	public String convert(String string) {
		
		String[] atts = string.split(INPUT_SPLITER_REGEX);
		if (atts.length == size) {
			StringBuilder sb = new StringBuilder(atts[0]);
			
			String title = atts[1];
			String desc1 = atts[2];
			String desc2 = atts[3];
			if (title != null) {				
				title = pattern.matcher(title).replaceAll(IdeaStringFilter.IDEA_REPLACEMENT);
			}
			if (desc1 != null) {				
				desc1 = pattern.matcher(desc1).replaceAll(IdeaStringFilter.IDEA_REPLACEMENT);
			}
			if (desc2 != null) {				
				desc2 = pattern.matcher(desc2).replaceAll(IdeaStringFilter.IDEA_REPLACEMENT);
			}
			
			sb.append(OUTPUT_SPLITER + title);
			title = generateKeyWord(title);
			if (title == null || title.length() == 0) {
				sb.append(OUTPUT_SPLITER + OUTPUT_NULL);
			} else {
				sb.append(OUTPUT_SPLITER + title);
			}
			
			sb.append(OUTPUT_SPLITER + desc1);
			desc1 = generateKeyWord(desc1);
			if (desc1 == null || desc1.length() == 0) {
				sb.append(OUTPUT_SPLITER + OUTPUT_NULL);
			} else {
				sb.append(OUTPUT_SPLITER + desc1);
			}
			
			sb.append(OUTPUT_SPLITER + desc2);
			desc2 = generateKeyWord(desc2);
			if (desc2 == null || desc2.length() == 0) {
				sb.append(OUTPUT_SPLITER + OUTPUT_NULL);
			} else {
				sb.append(OUTPUT_SPLITER + desc2);
			}
			
			//System.out.println(sb);
			return sb.toString();
		} else {
			return "error:"  + string;
		}
//		return null;
	}
	
	public String generateKeyWord(String str) {
		if (str != null) {
		List<String> keywords = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();
		char[] chars = str.toCharArray();
		// 符号{开始处，start为-1时说明尚未找到{
		int start = -1;
		// 上一次}的index
		int position = 0;
		for (int index = 0; index < chars.length; index++) {
			if (chars[index] == IDEA_KEYWORD_START && start == -1) {
				start = index;
			}
			if (start == -1) {
				position++;
			}
			if (chars[index] == IDEA_KEYWORD_END && start != -1) {
				if (start != index - 1) {
					String value = str.substring(start + 1, index);
					keywords.add(value);
					//last = index;
					position++;
				} else {
					position += 2;
				}
				start = -1;
			}
		}
		if (keywords.size() == 0) {
			return sb.toString();
		} else if (keywords.size() == 1) {
			sb.append(keywords.get(0));
			return sb.toString();
		} else {
			sb.append(keywords.get(0));
			for (int i = 1; i < keywords.size(); i++) {
				sb.append(OUTPUT_KEYWORD_SPLITER + keywords.get(i));
			}
			return sb.toString();
		}
		} else {
			return "";
		}
	
	}

}
